﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ECSE6770.Controllers
{
    [Authorize]
    [RequireHttps]
    public class MyController : BaseController
    {
        public ActionResult Index()
        {
            return View(ContextManager.CurrentUser);
        }

        public ActionResult ModuleFeedback(int id)
        {
            ViewBag.Module = Db.Modules.First(m => m.Id == id);
            return View();
        }

        public ActionResult Orders()
        {
            return View(OrderHistoryManager.CurrentUserOrders());
        }

        [HttpPost]
        public ActionResult SubmitFeedback(Feedback model)
        {
            if (ModelState.IsValid)
            {
                int productId = Convert.ToInt32(this.RouteData.Values["id"]);
                model.Product = Db.Products.First(p => p.Id == productId);
                return HandleFeedback(model);
            }
            else
                return View();
        }

        private ActionResult HandleFeedback(Feedback model)
        {
            model.UserInteropId = ContextManager.CurrentUser.Interop.Id; 
            model.DatePosted = DateTime.Now;
            Db.Feedback.Add(model);
            Db.SaveChanges();

            return View("FeedbackThanks");
        }
    }
}